<template>
  <view class="container">
    <u-form ref="form" :model="form">
      <u-form-item label="账号" prop="username">
        <u-input v-model="form.username" type="text" placeholder="请输入账号" />
      </u-form-item>
      <u-form-item label="密码" prop="password">
        <u-input
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
        />
      </u-form-item>
    </u-form>
    <u-button @click="submit">登录</u-button>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    async submit() {
      // 表单验证
      const validRes = await this.$refs.form.validate();
      if (validRes) {
        // 发起登录请求
        const loginRes = await this.login(this.form);
        if (loginRes.success) {
          // 登录成功处理
          console.log("登录成功", loginRes);
          // 跳转到首页或其他页面
          var userInfo = {
            account: this.form.username,
            password: this.form.password,
          };
          console.log("userInfo：", userInfo);
          // var openId = userInfo.openid;
          // userInfo = userInfo={"openid":"oTjKM1RdGBF_pFzBCUj1eXxDlJkk","nickname":"???","sex":0,"language":"","city":"","province":"","country":"","headimgurl":"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKJTU9jr88gaic5M8dF7qiaxIVLrY9zrNhB5GUIzyoCZS5icvP1Tu7kJbEbU3gko1rJOIKKicUPB5rdtQ/132","privilege":[]}
          uni.setStorageSync("userInfo", userInfo);
          uni.switchTab({
            url: "/pages/mall/index",
          });
        } else {
          // 登录失败处理
          console.log("登录失败", loginRes);
          // 提示用户错误信息
        }
      } else {
        console.log("表单验证失败");
      }
    },
    async login(credentials) {
      // 这里使用模拟的API请求进行登录
      // 实际项目中，你需要替换为真实的登录API
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ success: true, data: {} }); // 模拟登录成功
          // 实际项目中，根据服务器响应进行处理
        }, 1000);
      });
    },
  },
};
</script>
 
<style scoped>
.container {
  padding: 50px;
}
</style>